unit SettingsForm;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, 
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls,
  Vcl.Samples.Spin, Vcl.ComCtrls;

type
  TfrmSettings = class(TForm)
    pnlBottom: TPanel;
    btnOK: TButton;
    btnCancel: TButton;
    PageControl1: TPageControl;
    tsGeneral: TTabSheet;
    lblMaxThreads: TLabel;
    sedMaxThreads: TSpinEdit;
    lblRequestTimeout: TLabel;
    sedRequestTimeout: TSpinEdit;
    chkEnableCompression: TCheckBox;
    tsLogging: TTabSheet;
    lblLogLevel: TLabel;
    cbLogLevel: TComboBox;
    lblMaxLogSize: TLabel;
    sedMaxLogSize: TSpinEdit;
    lblLogRetention: TLabel;
    sedLogRetention: TSpinEdit;
    chkEnableAccessLog: TCheckBox;
    tsSecurity: TTabSheet;
    lblRateLimit: TLabel;
    sedRateLimit: TSpinEdit;
    lblRateWindow: TLabel;
    sedRateWindow: TSpinEdit;
    chkEnableHTTPS: TCheckBox;
    lblSSLPort: TLabel;
    sedSSLPort: TSpinEdit;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    edtServerName: TEdit;
    edtServerVersion: TEdit;
    chkAutoStart: TCheckBox;
    chkMinimizeToTray: TCheckBox;
    GroupBox2: TGroupBox;
    Label3: TLabel;
    Label4: TLabel;
    edtLogsFolder: TEdit;
    btnBrowseLogs: TButton;
    edtAccessLogFile: TEdit;
    chkEnablePerformanceLog: TCheckBox;
    chkEnableErrorLog: TCheckBox;
    GroupBox3: TGroupBox;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    edtCertFile: TEdit;
    btnBrowseCert: TButton;
    edtPrivateKey: TEdit;
    btnBrowseKey: TButton;
    edtCertPassword: TEdit;
    btnGenerateCert: TButton;
    btnTestSSL: TButton;
    tsModules: TTabSheet;
    Label8: TLabel;
    btnOpenModuleManager: TButton;
    procedure btnOKClick(Sender: TObject);
    procedure btnCancelClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure chkEnableHTTPSClick(Sender: TObject);
    procedure btnBrowseLogsClick(Sender: TObject);
    procedure btnBrowseCertClick(Sender: TObject);
    procedure btnBrowseKeyClick(Sender: TObject);
    procedure btnGenerateCertClick(Sender: TObject);
    procedure btnTestSSLClick(Sender: TObject);
    procedure btnOpenModuleManagerClick(Sender: TObject);
  private
    procedure LoadSettings;
    procedure SaveSettings;
    procedure UpdateSSLControls;
  public
    { Public declarations }
  end;

var
  frmSettings: TfrmSettings;

implementation

{$R *.dfm}

uses
  System.IniFiles, Vcl.FileCtrl, MainForm, ModulesForm;

procedure TfrmSettings.btnOKClick(Sender: TObject);
begin
  SaveSettings;
  ModalResult := mrOk;
end;

procedure TfrmSettings.btnCancelClick(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

procedure TfrmSettings.FormCreate(Sender: TObject);
begin
  LoadSettings;
  UpdateSSLControls;
end;

procedure TfrmSettings.LoadSettings;
var
  IniFile: TIniFile;
begin
  IniFile := TIniFile.Create(ChangeFileExt(ParamStr(0), '.ini'));
  try
    // 常规设置
    sedMaxThreads.Value := IniFile.ReadInteger('Server', 'MaxThreads', 100);
    sedRequestTimeout.Value := IniFile.ReadInteger('Server', 'RequestTimeout', 30);
    chkEnableCompression.Checked := IniFile.ReadBool('Server', 'EnableCompression', True);
    edtServerName.Text := IniFile.ReadString('Server', 'ServerName', 'Sparkle HTTP Server');
    edtServerVersion.Text := IniFile.ReadString('Server', 'ServerVersion', '1.0.0');
    chkAutoStart.Checked := IniFile.ReadBool('Server', 'AutoStart', False);
    chkMinimizeToTray.Checked := IniFile.ReadBool('Server', 'MinimizeToTray', False);
    
    // 日志设置
    cbLogLevel.ItemIndex := IniFile.ReadInteger('Logging', 'LogLevel', 1);
    sedMaxLogSize.Value := IniFile.ReadInteger('Logging', 'MaxLogSize', 10);
    sedLogRetention.Value := IniFile.ReadInteger('Logging', 'LogRetention', 30);
    chkEnableAccessLog.Checked := IniFile.ReadBool('Logging', 'EnableAccessLog', True);
    chkEnablePerformanceLog.Checked := IniFile.ReadBool('Logging', 'EnablePerformanceLog', True);
    chkEnableErrorLog.Checked := IniFile.ReadBool('Logging', 'EnableErrorLog', True);
    edtLogsFolder.Text := IniFile.ReadString('Logging', 'LogsFolder', '.\Logs\');
    edtAccessLogFile.Text := IniFile.ReadString('Logging', 'AccessLogFile', 'access.log');
    
    // 安全设置
    sedRateLimit.Value := IniFile.ReadInteger('Security', 'RateLimit', 1000);
    sedRateWindow.Value := IniFile.ReadInteger('Security', 'RateWindow', 1);
    chkEnableHTTPS.Checked := IniFile.ReadBool('Security', 'EnableHTTPS', False);
    sedSSLPort.Value := IniFile.ReadInteger('Security', 'SSLPort', 8443);
    edtCertFile.Text := IniFile.ReadString('Security', 'CertFile', '');
    edtPrivateKey.Text := IniFile.ReadString('Security', 'PrivateKey', '');
    edtCertPassword.Text := IniFile.ReadString('Security', 'CertPassword', '');
    
  finally
    IniFile.Free;
  end;
end;

procedure TfrmSettings.SaveSettings;
var
  IniFile: TIniFile;
begin
  IniFile := TIniFile.Create(ChangeFileExt(ParamStr(0), '.ini'));
  try
    // 常规设置
    IniFile.WriteInteger('Server', 'MaxThreads', sedMaxThreads.Value);
    IniFile.WriteInteger('Server', 'RequestTimeout', sedRequestTimeout.Value);
    IniFile.WriteBool('Server', 'EnableCompression', chkEnableCompression.Checked);
    IniFile.WriteString('Server', 'ServerName', edtServerName.Text);
    IniFile.WriteString('Server', 'ServerVersion', edtServerVersion.Text);
    IniFile.WriteBool('Server', 'AutoStart', chkAutoStart.Checked);
    IniFile.WriteBool('Server', 'MinimizeToTray', chkMinimizeToTray.Checked);
    
    // 日志设置
    IniFile.WriteInteger('Logging', 'LogLevel', cbLogLevel.ItemIndex);
    IniFile.WriteInteger('Logging', 'MaxLogSize', sedMaxLogSize.Value);
    IniFile.WriteInteger('Logging', 'LogRetention', sedLogRetention.Value);
    IniFile.WriteBool('Logging', 'EnableAccessLog', chkEnableAccessLog.Checked);
    IniFile.WriteBool('Logging', 'EnablePerformanceLog', chkEnablePerformanceLog.Checked);
    IniFile.WriteBool('Logging', 'EnableErrorLog', chkEnableErrorLog.Checked);
    IniFile.WriteString('Logging', 'LogsFolder', edtLogsFolder.Text);
    IniFile.WriteString('Logging', 'AccessLogFile', edtAccessLogFile.Text);
    
    // 安全设置
    IniFile.WriteInteger('Security', 'RateLimit', sedRateLimit.Value);
    IniFile.WriteInteger('Security', 'RateWindow', sedRateWindow.Value);
    IniFile.WriteBool('Security', 'EnableHTTPS', chkEnableHTTPS.Checked);
    IniFile.WriteInteger('Security', 'SSLPort', sedSSLPort.Value);
    IniFile.WriteString('Security', 'CertFile', edtCertFile.Text);
    IniFile.WriteString('Security', 'PrivateKey', edtPrivateKey.Text);
    IniFile.WriteString('Security', 'CertPassword', edtCertPassword.Text);
    
  finally
    IniFile.Free;
  end;
end;

procedure TfrmSettings.UpdateSSLControls;
begin
  // 根据HTTPS复选框状态更新SSL相关控件
  lblSSLPort.Enabled := chkEnableHTTPS.Checked;
  sedSSLPort.Enabled := chkEnableHTTPS.Checked;
  GroupBox3.Enabled := chkEnableHTTPS.Checked;
end;

procedure TfrmSettings.chkEnableHTTPSClick(Sender: TObject);
begin
  UpdateSSLControls;
end;

procedure TfrmSettings.btnBrowseLogsClick(Sender: TObject);
var
  Dir: string;
begin
  Dir := edtLogsFolder.Text;
  if SelectDirectory('Select Logs Folder', '', Dir) then
  begin
    edtLogsFolder.Text := IncludeTrailingPathDelimiter(Dir);
  end;
end;

procedure TfrmSettings.btnBrowseCertClick(Sender: TObject);
var
  OpenDialog: TOpenDialog;
begin
  OpenDialog := TOpenDialog.Create(nil);
  try
    OpenDialog.Filter := 'Certificate Files (*.crt, *.pem)|*.crt;*.pem|All Files (*.*)|*.*';
    OpenDialog.DefaultExt := 'crt';
    if OpenDialog.Execute then
    begin
      edtCertFile.Text := OpenDialog.FileName;
    end;
  finally
    OpenDialog.Free;
  end;
end;

procedure TfrmSettings.btnBrowseKeyClick(Sender: TObject);
var
  OpenDialog: TOpenDialog;
begin
  OpenDialog := TOpenDialog.Create(nil);
  try
    OpenDialog.Filter := 'Private Key Files (*.key, *.pem)|*.key;*.pem|All Files (*.*)|*.*';
    OpenDialog.DefaultExt := 'key';
    if OpenDialog.Execute then
    begin
      edtPrivateKey.Text := OpenDialog.FileName;
    end;
  finally
    OpenDialog.Free;
  end;
end;

procedure TfrmSettings.btnGenerateCertClick(Sender: TObject);
begin
  // 生成自签名证书的简化实现
  // 实际项目中应该调用OpenSSL或其他证书生成工具
  MessageDlg('This feature would generate a self-signed SSL certificate.'#13#10 +
             'In a real implementation, this would call OpenSSL or similar tool.',
             mtInformation, [mbOK], 0);
end;

procedure TfrmSettings.btnTestSSLClick(Sender: TObject);
begin
  // 测试SSL配置的简化实现
  if chkEnableHTTPS.Checked then
  begin
    if (edtCertFile.Text = '') or (edtPrivateKey.Text = '') then
    begin
      MessageDlg('Please specify both certificate file and private key file.', 
                 mtWarning, [mbOK], 0);
    end
    else if not FileExists(edtCertFile.Text) then
    begin
      MessageDlg('Certificate file does not exist.', mtError, [mbOK], 0);
    end
    else if not FileExists(edtPrivateKey.Text) then
    begin
      MessageDlg('Private key file does not exist.', mtError, [mbOK], 0);
    end
    else
    begin
      MessageDlg('SSL configuration appears to be valid.'#13#10 +
                 'Certificate: ' + edtCertFile.Text + #13#10 +
                 'Private Key: ' + edtPrivateKey.Text,
                 mtInformation, [mbOK], 0);
    end;
  end
  else
  begin
    MessageDlg('HTTPS is not enabled.', mtInformation, [mbOK], 0);
  end;
end;

procedure TfrmSettings.btnOpenModuleManagerClick(Sender: TObject);
begin
  // 打开模块管理器
  with TfrmModules.Create(Self) do
  try
    ShowModal;
  finally
    Free;
  end;
end;

end.